package com.think.teaching.controller;


import com.think.common.model.Result;
import com.think.common.model.ResultCode;
import com.think.teaching.model.TeacherInfo;
import com.think.teaching.service.impl.TeacherInfoServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 老师详细信息 前端控制器
 * </p>
 *
 * @author think
 * @since 2020-08-12
 */
@RestController
@RequestMapping("/v1")
@ResponseBody
@Api(tags = "老师信息管理接口")
public class TeacherInfoController {
	
	private final TeacherInfoServiceImpl teacherService;

	public TeacherInfoController(TeacherInfoServiceImpl teacherService) {
		this.teacherService = teacherService;
	}

	@ApiOperation("添加新的老师信息")
	@PostMapping("admin/teacher")
	public Result AddNewTeacherInfo(@RequestBody()TeacherInfo data) {
		Result result = data.CheckObjectComplete();
		if (result.getCode() == ResultCode.SUCCESS.code()) {
			TeacherInfo teacherInfo = teacherService.AddNewTeacher(data);
			if (teacherInfo == null) {
				result = Result.failure(ResultCode.SYSTEM_INNER_ERROR);
			}
			else {
				result = Result.success(teacherInfo);
			}
		}
		return result;
	}
	
	@ApiOperation("更新老师信息")
	@RequestMapping(value = "admin/teacher/update",method = RequestMethod.POST)
	public Result UpdateTeacherInfo(@RequestBody()TeacherInfo data) {
		Result result = data.CheckObjectComplete();
		if (result.getCode() == ResultCode.SUCCESS.code()) {
			TeacherInfo teacherInfo = teacherService.UpdateTeacher(data);
			if (teacherInfo == null) {
				result = Result.failure(ResultCode.SYSTEM_INNER_ERROR);
			}
			else {
				result = Result.success(teacherInfo);
			}
		}
		return result;
	}
	
	@ApiOperation("删除老师信息")
	@RequestMapping(value = "admin/teacher/del/{id}",method = RequestMethod.POST)
	public Result DeleteTeacherInfo(@PathVariable("id")Integer id) {
		Result result = Result.success();
		if (teacherService.getById(id) == null) {
			result = Result.failure(ResultCode.TEACHER_NOT_EXIST);
		}
		else {
			if (!teacherService.DeleteObjectById(id)) {
				result = Result.failure(ResultCode.SYSTEM_INNER_ERROR);
			}
		}
		return result;
	}
	
	@ApiOperation("管理员获取老师信息详情")
	@RequestMapping(value = "admin/teacher/{id}",method = RequestMethod.GET)
	public Result GetTeacherInfo(@PathVariable("id")Integer id) {
		TeacherInfo info = teacherService.GetTeacherInfo(id);
		Result result;
		if (info != null) {
			result = Result.success(info);
		}
		else {
			result = Result.failure(ResultCode.TEACHER_NOT_EXIST);
		}
		return result;
	}
	
	@ApiOperation("管理员修改老师信息状态")
	@RequestMapping(value = "admin/teacher/{id}/{state}",method = RequestMethod.PUT)
	public Result UpdateTeacherInfoState(@PathVariable("id")Integer id,@PathVariable("state")Integer state) {
		TeacherInfo info = teacherService.GetTeacherInfo(id);
		Result result;
		if (info != null) {
			result = Result.success(info);
		}
		else {
			try {
				info = teacherService.SetTeacherState(id, state);
				result = Result.success(info);//(ResultCode.STUDENT_NOT_EXIST);
			} catch (Exception e) {
				// TODO: handle exception
				result = Result.failure(ResultCode.SYSTEM_INNER_ERROR);
			}
			
		}
		return result;
	}
	
	@ApiOperation("用户搜索老师信息")
	@RequestMapping(value = "user/teacher/search",method = RequestMethod.GET)
	public Result UserSearchTeacherInfo(@RequestParam("key")String key) {
		return Result.success(teacherService.SearchTeacherWithKey(key, true));
	}
	
	@ApiOperation("管理员搜索老师信息")
	@RequestMapping(value = "admin/teacher/search",method = RequestMethod.GET)
	public Result AdminSearchTeacherInfo(@RequestParam("key")String key) {
		return Result.success(teacherService.SearchTeacherWithKey(key, false));
	}
	
	@ApiOperation("管理员搜索老师信息列表")
	@RequestMapping(value = "admin/teachers/{page}/{count}",method = RequestMethod.GET)
	public Result AdminListTeacherInfo(@PathVariable("page")Integer page,@PathVariable("count")Integer count) {
		return Result.success(teacherService.ListTeacherInfo(page, count, false));
	}
	
	@ApiOperation("用户搜索老师信息列表")
	@RequestMapping(value = "user/teachers/{page}/{count}",method = RequestMethod.GET)
	public Result UserListTeacherInfo(@PathVariable("page")Integer page,@PathVariable("count")Integer count) {
		return Result.success(teacherService.ListTeacherInfo(page, count, true));
	}
}

